//
//  SportMapLineView.m
//  RunningMan
//
//  Created by dllo on 16/5/26.
//  Copyright © 2016年 WSJ. All rights reserved.
//

#import "SportMapLineView.h"
#import <MapKit/MapKit.h>
@interface SportMapLineView ()<MKMapViewDelegate>

@property(nonatomic, retain)MKMapView *mapView;


@end


@implementation SportMapLineView
-(void)setLocationArr:(NSArray *)locationArr{
    
    if (_locationArr != locationArr) {
        _locationArr = locationArr;
    }
       [self drawWalkPolylineWithlocationArr:self.locationArr onMap:self.mapView];
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
//        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.frame.size.width / 2 - 100, self.frame.size.height - 30, 200, 30)];
//        label.text = @"行走线路";
//        label.textAlignment = 1;
//        label.textColor = [UIColor blueColor];
//        [self addSubview:label];
        
        [self addMapView];
    }
    return self;
}

-(void)addMapView{
    
    self.mapView = [[MKMapView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, self.bounds.size.height )];
    self.mapView.delegate = self;
    self.mapView.mapType = MKMapTypeStandard;
    [self addSubview:self.mapView];
    
 
}



- (void)drawWalkPolylineWithlocationArr:(NSArray *)locationsArr onMap:(MKMapView *)mapView
{
    
    // 轨迹点数组个数
    NSUInteger count = locationsArr.count;
    // 动态分配存储空间
    // MapPoint是个结构体：地理坐标点，用直角地理坐标表示 X：横坐标 Y：纵坐标
    MKMapPoint *tempPoints = malloc(sizeof(MKMapPoint) * locationsArr.count);
    // 遍历数组
    
    [locationsArr enumerateObjectsUsingBlock:^(CLLocation *location, NSUInteger idx, BOOL *stop) {
        MKMapPoint locationPoint = MKMapPointForCoordinate(location.coordinate);
        
        tempPoints[idx] = locationPoint;
    }];
    
    MKPolyline *polyLine = [MKPolyline polylineWithPoints:tempPoints count:count];
    //添加路线,绘图
    [mapView addOverlay:polyLine];
    // 清空 tempPoints 临时数组
    tempPoints = NULL;
    
    MKMapPoint northwestCoord; /*= CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT);*/
    MKMapPoint southnorthCoord; /*= CLLocationCoordinate2DMake(0, 0);*/
    for (int i = 0; i < locationsArr.count; i++) {
        if (i == 0) {
            CLLocation *location = locationsArr[0];
            MKMapPoint locpoint = MKMapPointForCoordinate(location.coordinate);
            northwestCoord = locpoint;
            southnorthCoord = locpoint;
        }else{
            CLLocation *temp = locationsArr[i];
            MKMapPoint temppoint = MKMapPointForCoordinate(temp.coordinate);
            northwestCoord.x = temppoint.x < northwestCoord.x ? temppoint.x : northwestCoord.x;
            northwestCoord.y = temppoint.y < northwestCoord.y ? temppoint.y : northwestCoord.y;
            southnorthCoord.x = temppoint.x > southnorthCoord.x ? temppoint.x : southnorthCoord.x;
            southnorthCoord.y = temppoint.y > southnorthCoord.y ? temppoint.y : southnorthCoord.y
            ;
        }
    }
    
    MKMapRect rect = MKMapRectMake(northwestCoord.x , northwestCoord.y , 10 + southnorthCoord.x - northwestCoord.x, 10 + southnorthCoord.y - northwestCoord.y);
    [mapView setVisibleMapRect:rect animated:YES];
    
}

- (MKOverlayRenderer *)mapView:(MKMapView *)mapView rendererForOverlay:(id<MKOverlay>)overlay
{
    if ([overlay isKindOfClass:[MKPolyline class]]) {
        MKPolylineRenderer *overlayRenderer = [[MKPolylineRenderer alloc] initWithPolyline:overlay];
        overlayRenderer.strokeColor = [UIColor redColor];
        overlayRenderer.fillColor = [UIColor blueColor];
        overlayRenderer.lineWidth = 2;
        return overlayRenderer;
    }
    return nil;
}



@end
